/**
 * 
 */
package com.cdhospital.admincontroller;

import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.cdhospital.Model.User;
import com.cdhospital.adminservice.LoginService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

/**
 * @author 17994
 *
 */
public class AdminLoginController extends Controller {
	 static LoginService service=new LoginService();
	public void index() {
		if(getSession().getAttribute("username")!=null){
			redirect("/admin/graduatemessage");
		}
		else
		render("index.html");
	}
	public void logins(){
		 String name = getPara("username");
	     String password = getPara("password");
	    
	     User  user=service.getUserByNamePasswd(name, password);
	     if (user != null) {
	            // 保存session
	            getSession().setAttribute("username", name);
	            setSessionAttr("username",name);
	            setAttr("err", 1);
	        } else {
	        	 setAttr("err", 2);
	        }

	        // 跳转到前台发起请求的路径
	        renderJson();


	      

	    }
	
	public void out(){
		//移除session
		getSession().removeAttribute("username");
		 String skey = (String) getSession().getAttribute("skey");
		 String sid = (String) getSession().getAttribute("wxsid");
		 String uid = (String) getSession().getAttribute("wxuin");
		 System.out.println(skey);
	     ss.sendPost("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout", "redirect=1&type=1&skey="+skey+"sid="+sid+"uin="+uid);
	     String a=  ss.sendPost("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout", "redirect=1&type=1&skey="+skey+"sid="+sid+"uin="+uid);
System.out.println(a);
		redirect("/admin/login");
	}
	

public void changemm(){
	String oldpassword=getPara("oldpassword");
	String newpassword=getPara("newpassword");
	 String name = (String) getSession().getAttribute("username");
	 User  user=service.getUserByNamePasswd(name, oldpassword);
	 if (user != null) {
		user.setPassword(newpassword);
		user.update();
		setAttr("err", 1);
	}
	 else{
		 setAttr("err", 2);
		 
	 }
renderJson();
}

public void weixinlogin() throws DocumentException{
	 //发送 GET 请求
    String s=ss.sendGet("https://login.weixin.qq.com/jslogin", "fun=new&lang=zh_CN&appid=wx782c26e4c19acffb");
    s=s.substring(s.indexOf("\"")+1, s.lastIndexOf("\""));
    String erweima="https://login.weixin.qq.com/qrcode/"+s;
    setAttr("err", erweima);
    setAttr("s", s);
     renderJson();
}
public void listen() throws DocumentException{
	 String s=getPara("s");
		System.out.println(s);
	  //获取二维码少秒状态   get
      //https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=gd94hc3_fg==&tip=0&r=-1160587432&_=1452859503803
      String satuts=null;
      boolean flag=true;
      String date=new Date().getTime()+"";
      while(flag){
    	 	 satuts=ss.sendGet("https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login", "loginicon=true&uuid="+s+"&tip=0&r=-"+date+""+"&_=1452859503803"); 
    	     String  satut=satuts.substring(satuts.indexOf("=")+1, satuts.indexOf(";"));
    	     if(satut.equals("200")){
    	    	 System.err.println("登录成功");
    	    	 flag=false;
    	    	break;
    	     }
    	     else if(satut.equals("201")){
    	    	 System.err.println("正在扫描");
    	     }
    	     else if(satut.equals("408")){
    	    	 setAttr("err", 2);
    	    	 System.err.println("已经超时");
    	    	 flag=true;
    	    	 break;
    	     	
    	     }
    	  }
    if(!flag){
     satuts=satuts.substring(satuts.indexOf("\"")+1, satuts.lastIndexOf("\""));
      String ticket=satuts.substring(satuts.indexOf("ticket")+7, satuts.indexOf("uuid")-1);
      String uuid=satuts.substring(satuts.indexOf("uuid")+5, satuts.indexOf("lang")-1);
      
    String message= ss. sendGet("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage", "ticket="+ticket+"&uuid="+uuid+"&lang=zh_CN&scan="+date+"&fun=new");
    Document dom=DocumentHelper.parseText(message);
    Element root=dom.getRootElement();
    String wxuin=root.element("wxuin").getText();
    String skey=root.element("skey").getText();
    String wxsid=root.element("wxsid").getText();
    setSessionAttr("skey", skey);
    setSessionAttr("wxsid", wxsid);
    setSessionAttr("wxuin", wxuin);
     Integer a=Integer.parseInt(wxuin);
   User user=service.getUserByWeixin(a);
   if(user!=null){
	   setSessionAttr("username",user.getUsername());
	   setAttr("err", 1);
   }
   else{
	   setAttr("err", 3);
   }
  
   renderJson();
    }
    else{
    	 setAttr("err", 2);
    	 System.err.println("已经超时");
    	 renderJson();
    }
}
}
